package controller;

import common.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
import ui.Main;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.Vector;

public class SelectPeopController implements Initializable {
    @FXML
    private ListView<User> selectedlist;
    private ObservableList<User> source=FXCollections.observableArrayList();
    private Select select;
    private Vector<User> student=new Vector<>();
    public SelectPeopController(Select select,Vector<User> student) {
        this.select=select;
        this.student.addAll(student);//学生名单，用与选人
        this.student.removeAll(select.getSelected());//将已经被选到的移除

    }

    public SelectPeopController(Select select) {
        try {
            this.select = select;
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        for(int i=0;i<select.getSelected().size();i++){
            source.add(select.getSelected().get(i));
        }
        selectedlist.setItems(source);
        selectedlist.setCellFactory(new Callback<ListView<User>, ListCell<User>>() {
            @Override
            public ListCell<User> call(ListView<User> param) {
                return new ListCell<User>() {
                    @Override
                    protected void updateItem(User user, boolean empty) {
                        super.updateItem(user, empty);
                        if (empty || user == null) {
                            setText(null);
                        } else {
                            setText(user.getName()+" - "+"学号"+user.getNumber());
                        }
                    }
                };
            }
        });
    }
    @FXML
    private Label error;
    private Socket socket= Main.userClientService.getSocket();
    @FXML
    public void select(){
        if(student.size()==0){
            error.setText("成员已全部被选中");
            return;
        }
        Random random = new Random();
        User randomElement =student.get(random.nextInt(student.size()));
        select.getSelected().add(randomElement);
        source.add(randomElement);
        student.remove(randomElement);
        try {
            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
            Message ms=new Message();
            ms.setState(MessageType.Select_Peop);
            oos.writeObject(ms);
            oos.writeObject(new SelectStu(select, randomElement.getId()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @FXML
    private Button selectButton;
    @FXML
    private Button del;
    @FXML
    public void del(){

    }
    @FXML
    public Button getSelection(){
        return selectButton;
    }
    @FXML
    public Button getDel(){
        return del;
    }
}
